--pieces
local base = piece("base")
  body = piece("body") 
    local engine = piece("engine")
    local turret, sleeve, barrel1, firepoint1 = piece("turret", "sleeve1", "barrel1", "firepoint1")
      local machinegun, firepoint2 = piece("machinegun", "firepoint2")

scriptParams = {
  smokePieces = { body },
  enginePieces = { engine },
  smokeDelay = 300,
  maxRockAngleX = 5,
  maxRockAngleZ = 5,
  rockOnMove = true,

  weapons = {
    [1] = {
	    --turret, sleeve, barrel, firepoint = 
      turretTurnSpeed = 90,
      sleeveTurnSpeed = 50,
	    barrelRecoil = 3,
	    rockForce = 150,
	    minPitch = -20,
	    restores = true,
      --restoreDelay = 3000,
	    --cegID = 1,      
      --openAnimation = true,	  
    },
    [2] = {
	    rotatesWith = turret,
      turretTurnSpeed = 180,
      sleeveTurnSpeed = 180,
	    minPitch = -20,
    },	
  }  
}

include "pre_scripts.lua"

setWeaponPieces(1, turret, sleeve, barrel1, firepoint1)
setWeaponPieces(2, machinegun, machinegun, machinegun, firepoint2)

include "scripts.lua"


--[[
StartBuilding (heading, pitch) 
	if heading < 0 - BARREL2_MAX_ANGLE_DIFF then heading = 0 - BARREL2_MAX_ANGLE_DIFF
	if heading > BARREL2_MAX_ANGLE_DIFF then heading = BARREL2_MAX_ANGLE_DIFF	
	if pitch < 0 - BARREL2_MAX_ANGLE_DIFF then pitch = 0 - BARREL2_MAX_ANGLE_DIFF
	if pitch > BARREL2_MAX_ANGLE_DIFF then pitch = BARREL2_MAX_ANGLE_DIFF	
	
	Turn (turret2, y_axis, heading, BARREL2_TURN_SPEED)
	Turn (sleeve2, x_axis, -pitch, BARREL2_TURN_SPEED)
	WaitForTurn (turret2, y_axis)
	WaitForTurn (sleeve2, x_axis)
	set INBUILDSTANCE to 1
end
]]--


function script.Killed (recentDamage, maxHealth)
  local severity = recentDamage / maxHealth
  
  Explode (engine, SFX.NONE)
  Explode (firepoint1, SFX.NONE)
  Explode (firepoint2, SFX.NONE)	
  if (severity <= 0.25) then
    Explode (body, SFX.NONE)
    Explode (turret, SFX.NONE)
    Explode (sleeve, SFX.NONE)
    Explode (barrel1, SFX.NONE)
    Explode (machinegun, SFX.FALL)
	  return 1
  elseif (severity <= 0.50) then
    Explode (body, SFX.NONE)
    Explode (turret, SFX.SMOKE + SFX.FIRE)
    Explode (sleeve, SFX.SMOKE + SFX.FIRE)  --SFX.NONE
    Explode (barrel1, SFX.SMOKE + SFX.FIRE)
    Explode (machinegun, SFX.SMOKE + SFX.FIRE)  
	  return 2
  else
    Explode (body, SFX.SHATTER)
    Explode (turret, SFX.SMOKE + SFX.FIRE + SFX.EXPLODE)
    Explode (sleeve, SFX.SMOKE + SFX.FIRE + SFX.EXPLODE)
    Explode (barrel1, SFX.SMOKE + SFX.FIRE + SFX.EXPLODE)
    Explode (machinegun, SFX.SMOKE + SFX.FIRE + SFX.EXPLODE)
	  return 3
  end
end